package operation

import (
	"../common"
	"../web"
	"strings"
)

type OperMergeObj struct {
	common.BaseHandler
}

func (self *OperMergeObj) Prepare() {}

func (self *OperMergeObj) ParameterCheck() bool {
	s_obox := strings.TrimSpace(self.Ctx.Params["nousebox"])
	s_nbox := strings.TrimSpace(self.Ctx.Params["newbox"])
	if s_obox == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Please input the Source Box.")
		return false
	}
	if s_nbox == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Please input the Merge Box.")
		return false
	}
	if s_obox == s_nbox {
		self.TmplData["error"] = common.GetText(self.Lang, "Can't Merge Box to itself.")
		return false
	}

	self.Sqlparas = append(self.Sqlparas, s_obox)
	self.Sqlparas = append(self.Sqlparas, s_nbox)
	self.Sqlparas = append(self.Sqlparas, self.Usr.Username)
	self.Flashparas = []interface{}{strings.ToUpper(s_obox), strings.ToUpper(s_nbox)}

	return true
}

func (self *OperMergeObj) SqlExecute() bool {
	spRtnData := *common.FetchOneRowPtr(
		`sp_MergeBox ?, ?, ?`,
		self.Usr.Conn, new(common.SPReturn), self.Sqlparas...)
	if spRtnData.(common.SPReturn).Flag > 0 {
		self.TmplData["error"] = common.GetTextSP(self.Lang, spRtnData)
		return false
	}
	return true
}

func MergeBoxHandler(ctx *web.Context) {
	self := new(OperMergeObj)
	self.Ctx = ctx
	self.Url = "/mergebox"
	self.Html = "mergebox.html"
	self.Rightlist = []string{"mergebox"}
	self.Flashstr = "Merge Box: From %v To %v finished."
	self.Do(self)
}
